#pragma  once 


template
<

	class ProductType,
	class Product

>
class CFactory
{
public:
	CFactory(void){}
	~CFactory(void){}

protected:
	template<class T >
	class Creator : public T
	{
	public:
		typename typedef  T type;
		static  void* Create()
		{ 
			return new type ;
		}	

	};

public:
	template<typename T>
	bool RegisterProduct(ProductType id)
	{

		lpfnCreator lf = Creator<T>::Create ;
		return m_ProductMap.insert(make_pair(id,lf)).second ;
	}

	bool UnRegister(ProductType id)
	{
		return m_ProductMap.erase(id) == 1 ; 
	}

	Product * CreateProduct(ProductType id)
	{
		ProductMap::iterator iter = m_ProductMap.find(id) ;

		if(iter != m_ProductMap.end())
		{
			return (Product *)((iter->second)());
		}

		return 0 ; // NULL;
	}

private:
	typedef  void* (*lpfnCreator)();
	typedef std::map<ProductType,lpfnCreator > ProductMap;

	ProductMap m_ProductMap;


};
